package tech.yangzhijie.basic.xml;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

public class XmlTest {

    public static void main(String[] args) {
        //mybatisXml();
        hello();
    }

    public static void hello(){
        SAXReader saxReader = new SAXReader();
        File file = new File("D:\\admin\\dev\\idea\\tests\\tests\\basic\\src\\main\\resources\\hello.xml");
        try {
            Document document = saxReader.read(file);
            Element rootElement = document.getRootElement();
            List<Element> elements = rootElement.elements();
            elements.forEach(e->{
                /*List<Attribute> attributes = e.attributes();
                attributes.forEach(f->{
                    System.out.println(f.getName()+":"+f.getValue());
                });*/
                List<Element> elements1 = e.elements();
                elements1.forEach(f->{
                    System.out.println(f.getName() + "  " + f.getText());
                });
            });
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public static void mybatisXml(){
        SAXReader saxReader = new SAXReader();
        File file = new File("D:\\admin\\dev\\idea\\tests\\tests\\basic\\src\\main\\resources\\mybatis-config.xml");
        try {
            Document document = saxReader.read(file);
            Element rootElement = document.getRootElement();
            Element element = rootElement.element("mappers");
            Element mapper = element.element("mapper");
            Attribute resource = mapper.attribute("resource");
            System.out.println(resource.getValue());
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}
